package com.example.online.music.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.online.music.common.R;
import com.example.online.music.mapper.CommentMapper;
import com.example.online.music.mapper.UserSupportMapper;
import com.example.online.music.model.domain.Comment;
import com.example.online.music.model.domain.UserSupport;
import com.example.online.music.model.request.UserSupportRequest;
import com.example.online.music.service.UserSupportService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserSupportServiceImpl extends ServiceImpl<UserSupportMapper, UserSupport>
        implements UserSupportService {

    @Autowired
    private UserSupportMapper userSupportMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public R isUserSupportComment(UserSupportRequest userSupportRequest) {
        QueryWrapper<UserSupport> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("comment_id", userSupportRequest.getCommentId());
        queryWrapper.eq("user_id", userSupportRequest.getUserId());
        if (userSupportMapper.selectCount(queryWrapper) > 0) {
            return R.success("您已取消点赞", true);
        } else {
            return R.success("您已点赞", false);
        }
    }

    @Override
    public R insertCommentSupport(UserSupportRequest userSupportRequest) {
        UserSupport userSupport = new UserSupport();
        BeanUtils.copyProperties(userSupportRequest, userSupport);
        if (userSupportMapper.insert(userSupport) > 0) {
            return R.success("添加记录成功");
        }
        return R.error("添加时,发生异常");
    }

    @Override
    public R deleteCommentSupport(UserSupportRequest userSupportRequest) {
        QueryWrapper<UserSupport> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("comment_id", userSupportRequest.getCommentId());
        queryWrapper.eq("user_id", userSupportRequest.getUserId());
        if (userSupportMapper.delete(queryWrapper) > 0) {
            return R.success("删除记录成功");
        }
        return R.error("删除记录发生异常");
    }

    /**
     * 点赞评论
     *  1、查询用户是否已评论，即：在点赞记录表（user-suport）中是否存在该记录；
     *   如果不存在：则说明用户未点赞，本次执行用户点赞逻辑；
     *   如果存在：则说明该用户已经点赞，本次执行用户取消点赞逻辑；
     * 【点赞逻辑】：
     *     1、先向点赞记录表中插入一条记录；
     *     2、查询该评论的点赞数，并进行 +1 更新操作；
     * 【取消点赞】：
     *     1、删除点赞记录表中记录；
     *     2、查询该评论的点赞数，并进行 -1 更新操作；
     *  3、返回最新的点赞数
     * @param userSupportRequest
     */
    @Override
    @Transactional //事务管理
    public R toSupport(UserSupportRequest userSupportRequest) {
        Comment comment = commentMapper.selectById(userSupportRequest.getCommentId());
        if(null != comment){
            int up = comment.getUp();
            QueryWrapper<UserSupport> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("comment_id", userSupportRequest.getCommentId());
            queryWrapper.eq("user_id", userSupportRequest.getUserId());
            UserSupport userSupport = userSupportMapper.selectOne(queryWrapper);
            if(null == userSupport){//不存在，则点赞
                userSupport = new UserSupport();
                BeanUtils.copyProperties(userSupportRequest, userSupport);
                userSupportMapper.insert(userSupport);
                commentMapper.updateUpById(comment.getId(), 1);
                return R.success("点赞成功", up + 1);
            }else{  //存在，则取消点赞
                userSupportMapper.delete(queryWrapper);
                commentMapper.updateUpById(comment.getId(), -1);
                return R.success("您已取消点赞", up - 1);
            }
        }else{
            return R.error("评论不存在或已被删除！");
        }
    }
}
